﻿namespace Cmn
{
    using System;
    using System.Diagnostics;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public class Safe
    {
        public static void doKill(string processName)
        {
            Process process = new Process();
            Process[] processesByName = Process.GetProcessesByName(processName);
            try
            {
                foreach (Process process2 in processesByName)
                {
                    if (!process2.CloseMainWindow())
                    {
                        process2.Kill();
                    }
                }
            }
            catch
            {
            }
        }

        public static string GetBrowser()
        {
            HttpBrowserCapabilities capabilities = new HttpBrowserCapabilities();
            return HttpContext.Current.Request.Browser.Type;
        }

        public static string GetIp()
        {
            string input = string.Empty;
            input = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            switch (input)
            {
                case null:
                case "":
                    input = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    break;
            }
            if ((input == null) || (input == string.Empty))
            {
                input = HttpContext.Current.Request.UserHostAddress;
            }
            if (!(((input != null) && (input != string.Empty)) && Valid.isIP(input)))
            {
                return "0.0.0.0";
            }
            return input;
        }

        public static string getLastUrl()
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
        }

        public static string getMd5(string str)
        {
            string s = str;
            string str3 = "";
            byte[] buffer = MD5.Create().ComputeHash(Encoding.Unicode.GetBytes(s));
            for (int i = 0; i < buffer.Length; i++)
            {
                str3 = str3 + buffer[i].ToString("x");
            }
            return str3;
        }

        public static string GetOS()
        {
            HttpBrowserCapabilities capabilities = new HttpBrowserCapabilities();
            return HttpContext.Current.Request.Browser.Platform;
        }

        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();
        }

        public static bool isRefresh(int seconds)
        {
            string input = Cookies.Get("__sysTime");
            if (input.Trim() != "")
            {
                if (!Valid.isDateTime(input))
                {
                    return true;
                }
                DateTime startTime = DateTime.Parse(input);
                DateTime now = DateTime.Now;
                if (Date.GetTimeSpan(startTime, now).Seconds >= seconds)
                {
                    return false;
                }
                Js.Alert("不允许刷新，或快速提交数据，请" + seconds.ToString() + "秒后提交数据。");
            }
            return true;
        }

        public static bool isSafeUrl(string doMain)
        {
            string[] strArray = doMain.Split(new char[] { '|' });
            foreach (string str in strArray)
            {
                if (HttpContext.Current.Request.UrlReferrer == null)
                {
                    return false;
                }
                if (HttpContext.Current.Request.UrlReferrer.Host.ToLower().Trim() == str.ToLower().Trim())
                {
                    return true;
                }
            }
            return false;
        }

        public static void setDateTime()
        {
            Cookies.Set("__sysTime", DateTime.Now.ToString());
        }

        public static string showHtml(string htmlStr)
        {
            string input = htmlStr.Replace("''", "'").Replace("\n", "<br />").Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").Replace("\0", "");
            input = new Regex("<script", RegexOptions.IgnoreCase).Replace(input, "<_script");
            input = new Regex("<object", RegexOptions.IgnoreCase).Replace(input, "<_object");
            input = new Regex("javascript:", RegexOptions.IgnoreCase).Replace(input, "_javascript:");
            input = new Regex("vbscript:", RegexOptions.IgnoreCase).Replace(input, "_vbscript:");
            input = new Regex("expression", RegexOptions.IgnoreCase).Replace(input, "_expression");
            input = new Regex("@import", RegexOptions.IgnoreCase).Replace(input, "_@import");
            input = new Regex("<iframe", RegexOptions.IgnoreCase).Replace(input, "<_iframe");
            input = new Regex("<frameset", RegexOptions.IgnoreCase).Replace(input, "<_frameset");
            return new Regex(" (on[a-zA-Z ]+)=", RegexOptions.IgnoreCase).Replace(input, " _$1=");
        }

        public static string sqlStr(string str)
        {
            str = str.Replace("'", "''");
            return str;
        }

        public static string unHtml(string htmlStr)
        {
            return htmlStr.Replace("''", "'").Replace("\n", "<br />").Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").Replace("&", "&amp;").Replace(">", "&gt;").Replace("<", "&lt;").Replace(" ", "&nbsp;").Replace("\0", "");
        }
    }
}

